#!/usr/bin/env node

const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');

async function runTest() {
  console.log(chalk.blue.bold('🧪 EPUB 转 PDF 工具 - 代码结构测试\n'));

  // 测试文件结构
  const requiredFiles = [
    'index.js',
    'package.json',
    'README.md',
    '.gitignore',
    'utils/fileUtils.js',
    'utils/epubParser.js',
    'converters/pdfConverter.js'
  ];

  console.log(chalk.yellow('📁 检查文件结构...'));

  let allFilesExist = true;
  for (const file of requiredFiles) {
    if (await fs.pathExists(file)) {
      console.log(chalk.green(`✅ ${file}`));
    } else {
      console.log(chalk.red(`❌ ${file} - 文件不存在`));
      allFilesExist = false;
    }
  }

  // 测试模块导入
  console.log(chalk.yellow('\n📦 测试模块导入...'));

  try {
    const { findEpubFiles } = require('./utils/fileUtils');
    console.log(chalk.green('✅ utils/fileUtils.js 导入成功'));
  } catch (error) {
    console.log(chalk.red(`❌ utils/fileUtils.js 导入失败: ${error.message}`));
  }

  try {
    const { extractEpub } = require('./utils/epubParser');
    console.log(chalk.green('✅ utils/epubParser.js 导入成功'));
  } catch (error) {
    console.log(chalk.red(`❌ utils/epubParser.js 导入失败: ${error.message}`));
  }

  try {
    const { convertHtmlToPdf, closeBrowser } = require('./converters/pdfConverter');
    console.log(chalk.green('✅ converters/pdfConverter.js 导入成功'));
  } catch (error) {
    console.log(chalk.red(`❌ converters/pdfConverter.js 导入失败: ${error.message}`));
  }

  // 检查 package.json
  console.log(chalk.yellow('\n📋 检查 package.json...'));

  try {
    const packageJson = require('./package.json');
    const requiredDeps = [
      'playwright',
      'fs-extra',
      'commander',
      'chalk',
      'ora',
      'adm-zip',
      'xmldom',
      'p-queue'
    ];
    
    for (const dep of requiredDeps) {
      if (packageJson.dependencies[dep]) {
        console.log(chalk.green(`✅ ${dep}: ${packageJson.dependencies[dep]}`));
      } else {
        console.log(chalk.red(`❌ ${dep} - 依赖缺失`));
      }
    }
  } catch (error) {
    console.log(chalk.red(`❌ package.json 解析失败: ${error.message}`));
  }

  console.log(chalk.blue.bold('\n🎉 测试完成！'));
  console.log(chalk.gray('注意：Playwright 需要网络连接来下载浏览器，如果网络有问题，可以稍后手动安装。'));
}

runTest().catch(console.error); 